// pages/mine/mine.ts

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      realname: "",
      avatar: ""
    },
    latitude: 26.66,
    longitude: 107.32222,
    goodsList: null, // generateGridList(10, 2)
    baseImgUrl: getApp().globalData.IMG_URL,
    storeGoodsList: Array,
    ui_storeJoin: "",
    ui_storeNearby: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.setData({
      latitude: getApp().globalData.latitude,
      longitude: getApp().globalData.longitude
    })

    this.onPullDownRefresh()

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const user = wx.getStorageSync('userInfo');
    // console.error(JSON.parse(user));
    this.setData({ userInfo: JSON.parse(user) });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // this.getHotGoodsPage()
    this.getNearbyStorePage()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  doing() {

  },
  getCurrAvatar() {
    return "http://emall.asiakylin.com:8888/assets/logo.5b45a293.png"
  },
  gotoGoodsDetail(param: any): void { // 打开商品详情
    wx.navigateTo({
      url: '/pages/goods/goodsDetail/goodsDetail',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { goodsId: param.currentTarget.dataset.id })
      }
    })
  },
  doScanCode(): void {
    // console.error("扫码结果：doScanCode")
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        //   console.error("扫码结果：" + JSON.stringify(res))
        let path = res.path
        let pagePath: String = path.substring(0, path.indexOf("?"))
        let param: String = path.substring(path.indexOf("=") + 1)
        // console.error("扫码结果： pagePath = " + pagePath)
        // console.error("扫码结果：param = " + param)
        wx.redirectTo({ url: "/" + pagePath + '?scene=' + param })
      },
      error(res: any) {
        console.error("扫码结果：" + JSON.stringify(res))
      }
    })
  },
  gotoPage(param: any): void {
    let pagePath = param.currentTarget.dataset.path
    // console.error("pagePath: " + pagePath)
    wx.navigateTo({
      url: pagePath,
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据  
        res.eventChannel.emit('acceptDataFromOpenerPage', { id: "test" })
      }
    })
  },
  getHotGoodsPage() {
    var that = this
    // const token = wx.getStorageSync('token')
    const preUrl = getApp().globalData.SERVER_URL

    const goodsPageForHomeUrl = preUrl + "fc/goods/client/anon/pageForHomeList"
    wx.request({
      url: goodsPageForHomeUrl,
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: 10
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        // console.error("结果: " + res)
        // console.error("结果data: " + res.data)

        wx.stopPullDownRefresh()
        if (res.data.code == 200) {
          const length = res.data.result.records.length
          for (let index = 0; index < length; index++) {
            const item = res.data.result.records[index]
            if (item != null) {
              const cover = item.goodsCover;
              // console.error("cover ==> " + cover)
              const firstIndex = cover.indexOf(',');
              const coverReal = cover.substring(0, firstIndex);
              // console.error("coverReal ==> " + coverReal)
              item.goodsCover = coverReal

              if (item.goodsName.length > 16) {
                item.goodsName = item.goodsName.substring(0, 16) + "...";
              }
            }
          }
          that.setData({ goodsList: res.data.result.records })
          // console.info(JSON.stringify(that.data.goodsList))
        } else {
          console.info(JSON.stringify(res))
        }
      },
    })
  },
  getNearbyStorePage() {
    // console.error("getNearbyStorePage：this.data: " + JSON.stringify(this.data))
    var that = this
    const preUrl = getApp().globalData.SERVER_URL
    const url = preUrl + "fc/store/client/anon/pageForType"
    wx.request({
      url: url,
      method: 'GET',
      data: {
        pageNo: 1,
        pageSize: 100,
        type: 'nearbyStore',
        locLat: this.data.latitude,
        locLng: this.data.longitude
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded', // 默认值
        // 'X-Access-Token': token
      },
      success(res: any) {
        // console.error("getNearbyStorePage：结果: " + JSON.stringify(res))
        wx.stopPullDownRefresh()
        if (res.data.code == 200) {

          that.setData({ storeGoodsList: res.data.result.records })

          //   const length = res.data.result.records.length
          //   for (let index = 0; index < length; index++) {
          //     const item = res.data.result.records[index]
          //     if (item != null) {
          //       const cover = item.goodsCover;
          //       // console.error("cover ==> " + cover)
          //       const firstIndex = cover.indexOf(',');
          //       const coverReal = cover.substring(0, firstIndex);
          //       // console.error("coverReal ==> " + coverReal)
          //       item.goodsCover = coverReal

          //       if (item.goodsName.length > 16) {
          //         item.goodsName = item.goodsName.substring(0, 16) + "...";
          //       }
          //     }
          //   }
          //   that.setData({ goodsList: res.data.result.records })
          // console.info(JSON.stringify(that.data.goodsList))
        } else {
          console.info(JSON.stringify(res))
        }
      },
      complete(){
        that.setData({ui_storeJoin: "关于应用",ui_storeNearby: "附近推荐"})
      }
    })
  },
  // gotoStoreDetailPage(param: any): void {
  //   let pagePath = param.currentTarget.dataset.path
  //   let storeId = param.currentTarget.dataset.tenantid
  //   console.error("storeId: " + storeId)
  //   wx.navigateTo({
  //     url: pagePath,
  //     events: {
  //     },
  //     success: function (res) {
  //       // 通过eventChannel向被打开页面传送数据  
  //       res.eventChannel.emit('acceptDataFromOpenerPage', { storeId: storeId })
  //     }
  //   })
  // },

  /**
    * 打开详情-商家
    */
  toStoreDetail(param: any) {
    const bean = param.currentTarget.dataset.bean
    let storeId = bean.id
    console.error("storeId: " + storeId)
    wx.navigateTo({
      url: '/pages/apps/store/storeHome/storeHome',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据  
        res.eventChannel.emit('acceptDataFromOpenerPage', { storeId: storeId })
      }
    })
  },

  gotoOrderTabs(param: any): void {
    let type = param.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/order/orderTabs/orderTabs',
      events: {
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据  
        res.eventChannel.emit('pageData', { type: type })
      }
    })
  },

})